<?php


namespace app\man\controller;
use think\Controller;
use think\Db;
use think\Paginator;
use think\Cache;
use think\Request;
use think\Session;
class Vendor extends Common
{
	public function dlist(){
		if(!$data=cache::store($this->Getcache())->get('osman_vendor_data')){
			$data = array('appid'=>'appId','auth_con'=>'校验文件内容','auth_name'=>'校验文件名称');
			cache::store($this->Getcache())->set('osman_vendor_data',$data,180);
		}
		 
		$domains = Db::name("vendor")->order('id asc')->paginate();
		$this->assign('data',$data);
		$this->assign('list',$domains);

		return $this->fetch('list');
	}
	private function Getcache(){
		$cache = 'redis';
		if(Request::instance()->domain()=='http://osman.cn'){
			$cache = 'file';
		}
		return $cache;
	}

	public function add(){

		return $this->fetch('add');
	}
	public function doadd()
	{

		$domains = input('domains');
		$appid = input('appid');
		$appkey = input('appkey');
		$lists = explode("\n",$domains);
		foreach($lists as $k=>$val){
			$data[$k]['domain']=trim($val);
			$data[$k]['status']=1;
			$data[$k]['accessKeyId']=$appid;
		}
		$result = Db::name("vendor")->insertAll($data);
		if($result){
			$this->success('添加成功', url('man/vendor/add'), 3);
		}else{
			$this->success('添加失败');
		}
	}

	public function change(){
		$aid=input('aid');
		$status=input('status');
		if($status==2){
			$res = Db::name("vendor")->where('id',$aid)->update(['status'=>$status,'start_time'=>date("Y-m-d H:i:s")]);
		}else{
			$res = Db::name("vendor")->where('id',$aid)->update(['status'=>$status]);
		}

		if($status==3){
			$res = Db::name("vendor")->where('id',$aid)->update(['status'=>$status,'end_time'=>date("Y-m-d H:i:s")]);
		}else{
			$res = Db::name("vendor")->where('id',$aid)->update(['status'=>$status]);
		}
		if($status ==1){
			$res = Db::name("vendor")->where('id',$aid)->update(['status'=>$status]);
		}

		if($res===false){
			return false;
		}else{
 
		// 	$options = [
  //           // 缓存类型为File
  //           'type'  =>  'Redis', 
  //           // 缓存有效期为永久有效
  //           'expire'=>  180, 
  //           //缓存前缀
  //           'prefix'=>  'think',
  //            // 指定缓存目录
  //           'host'  =>  '127.0.0.1',

  //           'port'  => '6379'
  //       ];
  //       Cache::connect($options);
		// cache::rm("vendor");
		// cache::rm("osman_vendor_mp");
		Cache::store('redis')->set('vendor',NULL,1);
		Cache::store('redis')->set('osman_vendor_mp',NULL,1);
		return true;
		}
	}

	public function delete(){
		$id = input('id');
		$res = Db::name('vendor')->where('id',$id)->delete();
		if($res){
			$this->success('删除成功',url('man/vendor/dlist'));	
		}else{
			$this->success('删除失败',url('man/vendor/dlist'));
		}
	}
	public function deleteAliyun(){
		$id = input('id');
		$bucket = input('bucket');
		$OssClient = new \OSS\OssClient('LTAIVccN6OiPIQF9', '6Kf3sxc0pgMbd0TQXV1Kv6DNBGSnGL', 'oss-cn-shanghai.aliyuncs.com', false);
		$res = $OssClient->deletBucket($bucket);
		dump($res);
	}
	public function createAliyun(){
		// $data['appid'] = input('appid');
		// $data['auth_con'] = input('auth_con');
		// $data['auth_name'] = input('auth_name');
		// cache::store($this->Getcache())->set('osman_vendor_data',$data,180);
		// $OssClient = new \OSS\OssClient('LTAIVccN6OiPIQF9', '6Kf3sxc0pgMbd0TQXV1Kv6DNBGSnGL', 'oss-cn-shanghai.aliyuncs.com', false);
		// $bucket = createrand(8);
		// $bucket = $this->createrand();
  //       $acl = \OSS\OssClient::OSS_ACL_TYPE_PUBLIC_READ;
  //       try {
  //           $OssClient->createBucket($bucket, $acl);
  //       } catch (OssException $e) {

  //           $message = $e->getMessage();
  //           if (\OSS\Core\OssUtil::startsWith($message, 'http status: 403')) {
  //               echo "Please Check your AccessKeyId and AccessKeySecret" . "\n";
  //               exit(0);
  //           } elseif (strpos($message, "BucketAlreadyExists") !== false) {
  //               echo "Bucket already exists. Please check whether the bucket belongs to you, or it was visited with correct endpoint. " . "\n";
  //               exit(0);
  //           }
  //           printf(__FUNCTION__ . ": FAILED\n");
  //           printf($e->getMessage() . "\n");
  //           return;
  //       }

		// $dd=$OssClient->multiuploadFile($bucket,'index.html',ROOT_PATH.'public/static/index.html',array());
		// $OssClient->multiuploadFile($bucket,'favicon.ico',ROOT_PATH.'public/favicon.ico',array());
		// $OssClient->putObject($bucket,$data['auth_name'].'.txt',$data['auth_con']);
		
		// if($dd){
		// 	Db::name('vendor')->insert(['domain'=>$dd['oss-requestheaders']['Host'],'accessKeyId'=>$data['appid'],'status'=>1,'bucket'=>$bucket,'endpoint'=>$data['auth_name']]);
		// 	return true;
		// }
		// return false;
 
		Cache::store('redis')->set('vendor',NULL,1);
		Cache::store('redis')->set('osman_vendor_mp',NULL,1);
		return true;
	}

	public function createrand(){

		$str = 'abcdefghijklmnopqrstxyzuvw1234567890';
		$rand = '';
		for($i=0;$i<8;$i++){
			$rand .= substr($str,mt_rand(0,strlen($str) -1),1);
		}
		return $rand;
	}

	public function cblur(){
		$nid = input('aid');

		$bek = input('appid');

		$res = Db::name('vendor')->where('id',$nid)->update(['accessKeyId'=>$bek]);

		if($res){
			echo true;
		}else{
			echo false;
		}
	}
}